﻿using System;
using System.Collections;
using System.Data;
using visinia.SmartEngine.Cache;
using visinia.SmartEngine.Providers;

namespace visinia.SmartEngine.Site
{
    /// <summary>
    /// The SiteSettings is the website settings controller class
    /// </summary>
    public class SiteSettings
    {
        private const string KEY_SITE_SETTINGS = "siteSettings";

        /// <summary>
        /// The visinia CMS version
        /// </summary>
        public static string visiniaVersion
        {
            get { return "1.5"; }          
        }

        /// <summary>
        /// The website name
        /// </summary>
        public static string WebsiteName
        {
            get { return (string)Settings["WebsiteName"]; }
            set { AddSetting("WebsiteName", value); }
        }

        /// <summary>
        /// A line about the website
        /// </summary>
        public static string WebsiteTagLine
        {
            get { return (string)Settings["WebsiteTagLine"]; }
            set { AddSetting("WebsiteTagLine", value); }
        }      
        
        /// <summary>
        /// The site front page id
        /// visinia front page can be set to any page and 
        /// can be changed at any time.
        /// </summary>        
        public static int FrontPage
        {
            get { return Convert.ToInt32(Settings["FrontPage"]); }
            set { AddSetting("FrontPage", value.ToString()); }
        }

        /// <summary>
        /// The active admin theme
        /// </summary>
        public static string AdminTheme
        {
            get { return (string)Settings["AdminTheme"]; }
            set { AddSetting("AdminTheme", value); }
        }

        /// <summary>
        /// The active website theme
        /// </summary>
        public static string Theme
        {
            get { return (string)Settings["Theme"]; }
            set { AddSetting("Theme", value); }
        }

        /// <summary>
        /// Gets the site setting
        /// </summary>
        /// <param name="key">The settings key to get</param>
        /// <returns>returns the setting value</returns>
        public static string GetSetting(string key)
        {
            if (Settings.Contains(key))
                return (string)Settings[key];
            else
                return string.Empty;
        }

        private static Hashtable _Settings;
        private static Hashtable Settings
        {
            get
            {
                _Settings = (Hashtable)DataCache.GetCache(KEY_SITE_SETTINGS);
                if (_Settings == null)
                {
                    _Settings = new Hashtable();
                    IDataReader dr = SmartEngineDataProvider.Provider.GetSiteSettings();
                    while (dr.Read())
                    {
                        if (dr["SettingName"] != Convert.DBNull)
                        {
                            if (dr["SettingValue"] != Convert.DBNull)
                                _Settings[(string)dr["SettingName"]] = (string)dr["SettingValue"];
                            else
                                _Settings[(string)dr["SettingName"]] = "";
                        }
                    }
                    dr.Close();
                    DataCache.SetCache(KEY_SITE_SETTINGS, _Settings);
                }
                return _Settings;
            }
        }

        #region DATA
        /// <summary>
        /// Adds a new site setting Or updates it if it already exists
        /// </summary>
        /// <param name="key">The key to setting</param>
        /// <param name="value">The value of the setting</param>
        public static void AddSetting(string key, string value)
        {
            if (Settings.ContainsKey(key))
                SmartEngineDataProvider.Provider.UpdateSiteSetting(key, value);
            else
                SmartEngineDataProvider.Provider.AddSiteSetting(key, value);
            DataCache.DeleteCacheMultiple("site");
        }
        /// <summary>
        /// Deletes the site setting
        /// </summary>
        /// <param name="key">The key to delete</param>
        public static void DeleteSetting(string key)
        {
            SmartEngineDataProvider.Provider.DeleteSiteSetting(key);
            DataCache.DeleteCacheMultiple("site");
        }
        #endregion
    }
}
